<?php

if($_POST["typeEnt"] == _CASH_BANK_IN){

  if($_POST["txEnt"] == _CASH_BANK_DP_NAME){

    /**
     * check out if entry exists then
     * it is a modified type
     * then clean up
     */
    $CLFetchers->fetchDtlDp($Db,
                            $_SESSION["tmpJid"],
                            $SourceDb->fetchValue("srcDbTmpDtlDp"),
                            $nData,
                            $lstData);

    if($nData > 0){

      $CHandlers->dbEntDelete($Db,
                              $_SESSION["tmpJid"],
                              $SourceDb->fetchValue("srcDbTmpSubDp"));

      $CHandlers->dbEntDelete($Db,
                              $_SESSION["tmpJid"],
                              $SourceDb->fetchValue("srcDbTmpDtlDp"));
    }else{

      /*Do nothing*/
    }

    for($iRow = 0; $iRow < $_SESSION["init_sub"] ; $iRow++){

      /**
       * fetch the cash bank out reference
       * so we could have the cash bank code
       * and cash bank tx type
       */
      $CLFetchers->fetchSourceHeader($Db,
                                     $_POST["notaSrcEnt"][$iRow],
                                     $SourceDb->fetchValue("srcDbSourceHdr"),
                                     $nHeader,
                                     $resHeader);

      /**
       * fetch the cash bank tx type
       */
      $CLFetchers->fetchCbTxType($Db,
                                 $resHeader["C_B_CODE"][0],
                                 $SourceDb->fetchValue("srcDbMstCb"),
                                 $cbTxType);

      $CLocal->CbTxType($_POST["typeEnt"],
                        $cbTxType,
                        $txType);

      $txDate  = " TO_DATE('".$_POST["trDateEnt"][$iRow]."','dd-mm-YYYY') ";
      $dtxDate = " TO_DATE('".$_POST["dueDateEnt"][$iRow]."','dd-mm-YYYY') ";

      $values = " '".$_SESSION["tmpJid"]."',          ".
                "  ".($iRow+1)." ,                    ".
                "  ".$curPeriode." ,                  ".
                " '".$_POST["notaSrcEnt"][$iRow]."',  ".
                "  ".$txDate." ,                      ".
                "  ".$dtxDate." ,                     ".
                " '".$_GET["whoseCode"]."',           ".
                " '".$_POST["currencyEnt"][$iRow]."', ".
                "  ".$_POST["rateEnt"][$iRow]." ,     ".
                "  ".$_POST["remainEnt"][$iRow]." ,   ".
                "  ".$_POST["receiveEnt"][$iRow]." ,  ".
                "  ".$txType."                        ";

      $CLHandlers->insToSubDp($Db,
                              $values,
                              $SourceDb->fetchValue("srcDbTmpSubDp"));
    }

    $values = " '".$_SESSION["tmpJid"]."',   ".
              "  ".$_POST["totInvoice"]." ,  ".
              "  ".$_GET["totSrcNet"]." ,    ".
              "  ".$_POST["gainEnt"]." ,     ".
              "  ".$_POST["discEnt"]."       ";

    $CLHandlers->insToDtlDp($Db,
                            $values,
                            $SourceDb->fetchValue("srcDbTmpDtlDp"));
  }else
  if($_POST["txEnt"] == _CASH_BANK_CCL_DP){


  }else{

    $Misc->usrDieJs("Error: Unknown Tx");
  }
}else
if($_POST["typeEnt"] == _CASH_BANK_OUT){

  if($_POST["txEnt"] == _CASH_BANK_DP_NAME){

    /**
     * check out if entry exists then
     * it is a modified type
     * then clean up
     */
    $CLFetchers->fetchDtlDp($Db,
                            $_SESSION["tmpJid"],
                            $SourceDb->fetchValue("srcDbTmpDtlDp"),
                            $nData,
                            $lstData);

    if($nData > 0){

      $CHandlers->dbEntDelete($Db,
                              $_SESSION["tmpJid"],
                              $SourceDb->fetchValue("srcDbTmpSubDp"));

      $CHandlers->dbEntDelete($Db,
                              $_SESSION["tmpJid"],
                              $SourceDb->fetchValue("srcDbTmpDtlDp"));
    }else{

      /*Do nothing*/
    }

    for($iRow = 0; $iRow < $_SESSION["init_sub"] ; $iRow++){

      /**
       * fetch the cash bank out reference
       * so we could have the cash bank code
       * and cash bank tx type
       */
      $CLFetchers->fetchSourceHeader($Db,
                                     $_POST["notaSrcEnt"][$iRow],
                                     $SourceDb->fetchValue("srcDbSourceHdr"),
                                     $nHeader,
                                     $resHeader);

      /**
       * fetch the cash bank tx type
       */
      $CLFetchers->fetchCbTxType($Db,
                                 $resHeader["C_B_CODE"][0],
                                 $SourceDb->fetchValue("srcDbMstCb"),
                                 $cbTxType);

      $CLocal->CbTxType($_POST["typeEnt"],
                        $cbTxType,
                        $txType);

      $txDate  = " TO_DATE('".$_POST["trDateEnt"][$iRow]."','dd-mm-YYYY') ";
      $dtxDate = " TO_DATE('".$_POST["dueDateEnt"][$iRow]."','dd-mm-YYYY') ";

      $values = " '".$_SESSION["tmpJid"]."',          ".
                "  ".($iRow+1)." ,                    ".
                "  ".$curPeriode." ,                  ".
                " '".$_POST["notaSrcEnt"][$iRow]."',  ".
                "  ".$txDate." ,                      ".
                "  ".$dtxDate." ,                     ".
                " '".$_GET["whoseCode"]."',           ".
                " '".$_POST["currencyEnt"][$iRow]."', ".
                "  ".$_POST["rateEnt"][$iRow]." ,     ".
                "  ".$_POST["remainEnt"][$iRow]." ,   ".
                "  ".$_POST["paymentEnt"][$iRow]." ,  ".
                "  ".$txType."                        ";

      $CLHandlers->insToSubDp($Db,
                              $values,
                              $SourceDb->fetchValue("srcDbTmpSubDp"));
    }

    $values = " '".$_SESSION["tmpJid"]."',   ".
              "  ".$_POST["totInvoice"]." ,  ".
              "  ".$_GET["totSrcNet"]." ,    ".
              "  ".$_POST["gainEnt"]." ,     ".
              "  ".$_POST["discEnt"]."       ";

    $CLHandlers->insToDtlDp($Db,
                            $values,
                            $SourceDb->fetchValue("srcDbTmpDtlDp"));
  }else
  if($_POST["txEnt"] == _CASH_BANK_CCL_DP){


  }else{

    $Misc->usrDieJs("Error: Unknown Tx");
  }
}else{

  $Misc->usrDieJs("Error: Unknown Type");
}

$Db->commit();

if($Db->getStatus()){

  echo  $GJs->jsScript(_TYPE_JS,"this.window.close();");
}else{

  $Misc->usrMsgJs("Upload Process Interupted");
}

?>